package com.jwdf.uda.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;

/**
 * site :  http://www.andcup.com
 * email:  amos@andcup.com
 * github: https://github.com/andcup
 * Created by Amos on 2016/3/15.
 */
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<BaseViewHolder>{

    protected Context mContext;
    protected LayoutInflater mInflater;
    protected List<T> mData = new ArrayList();
    private static final int ITEM_TEXT = 0;
    private static final int ITEM_SINGLE_IMAGE = 1;
    private static final int ITEM_MULTI_IMAGE = 2;
    private int mPosition;



    public abstract BaseViewHolder onCreateViewHolder(ViewGroup parent, int mPosition);

    public BaseAdapter(Context context){
        mContext  = context;
        mInflater = LayoutInflater.from(mContext);
    }


    public void append(List<T> items) {
        if (items != null && !items.isEmpty()) {
            mData.addAll(items);
            notifyDataSetChanged();
        }
    }

    public void add(T item) {
        if (item != null) {
            mData.add(item);
            notifyDataSetChanged();
        }
    }

    public void remove(T item) {
        if (item != null) {
            mData.remove(item);
            notifyDataSetChanged();
        }
    }

    public void remove(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }

    public void clear() {
        mData.clear();
        notifyDataSetChanged();
    }

    public void setData(List<T> items) {
        if (items != null) {
            mData.clear();
            mData.addAll(items);
            notifyDataSetChanged();
        }
    }

    public int getCount() {
        return mData.size();
    }

    public boolean isEmpty() {
        return getCount() == 0;
    }

    @Override
    public final void onBindViewHolder(BaseViewHolder holder, int position) {
        T item;
        try {
            item = mData.get(position);
        }catch (Exception e){
            item = mData.get(0);
        }
        mPosition=position;
        holder.onBindView(item,position);
    }

}
